﻿using System;

namespace Validate.Net
{
    /// <summary>
    /// 值区间校验
    /// </summary>
    public class ValidateRangeValueAttribute : AbstractValidateAttribute
    {
        private double minLeng;
        private double maxLeng;

        public ValidateRangeValueAttribute(double minLeng, double maxLeng)
        {
            this.minLeng = minLeng;
            this.maxLeng = maxLeng;
        }

        public override bool Validate(object oValue, out string msg)
        {
            Type type = oValue?.GetType();
            if (!object.ReferenceEquals(typeof(int), type) &&
                !object.ReferenceEquals(typeof(long), type) &&
                !object.ReferenceEquals(typeof(double), type))
            {
                msg = $"特性[{this.GetType().Name}]只能校验数字类型";
                return false;
            }
            if (double.TryParse(oValue.ToString(), out double value)
                && value >= this.minLeng
                && value <= this.maxLeng)
            {
                msg = string.Empty;
                return true;
            }
            else
            {
                msg = $"值范围为：[{this.minLeng}—{this.maxLeng}]";
                return false;
            }
        }
    }
}
